/*
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.com.app.knock;

import java.io.IOException;

import org.com.app.knock.activity.SplashActivity;
import org.json.JSONException;
import org.json.JSONObject;

import z.lib.base.BaseUtils;
import z.lib.base.LogUtils;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Vibrator;
import android.provider.Settings.Secure;
import android.util.Log;

import com.app.knock.db.AvataList;
import com.app.knock.db.IconList;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.knock.app.R;

/**
 * Handling of GCM messages.
 */
public class GcmBroadcastReceiver extends BroadcastReceiver {
	static final String TAG = "GCMDemo";
	public static final int NOTIFICATION_ID = 1;
	private boolean useBrodcast = false;
	private static final String ACTION = "com.app.push.mPaserPushBroadcastReciver";

	@Override
	public void onReceive(Context context, Intent intent) {
		GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
		String message = intent.getExtras().getString("data");
		LogUtils.e(TAG, message);
//		{"icon":{"id":"4"},"time":1426243294,"UserFrom":{"id":"1","name":"hahuy8692","avatar":"","avatar_id":"3"}}
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		int icon = R.drawable.ic_launcher;
		String title = "title";
		String description = "description";
		long time = System.currentTimeMillis();

		String notificationTitle = "notificationTitle";
		Notification notification = new Notification(icon, notificationTitle, time);
		notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL;
		PendingIntent contentIntent = null;

//		int idPush = 0;
//
//		Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
//		// Vibrate for 300 milliseconds
//		v.vibrate(300);
//
//		Intent notificationIntent = new Intent(context, SplashActivity.class);
//		contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
//		
//		notification.setLatestEventInfo(context, notificationTitle, description, contentIntent);
//		mNotificationManager.notify(idPush, notification);
		
		try {
			String iconid = "0";
			String UserFrom = "";
			try {
				iconid = new JSONObject(message).getJSONObject("icon").getString("id");
				UserFrom = new JSONObject(message).getJSONObject("UserFrom").getString("name");
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
//			JSONObject temp = new JSONObject(message);
			String message1 = context.getString(R.string.send_from) +" " + UserFrom /* +temp.getJSONObject("UserFrom").getString("name")*/;
			String customdata =  new IconList(context).getIcon(iconid)/*BaseUtils.API.BASESERVERROOT + "uploads/images/"+temp.getJSONObject("icon").getString("url")*/;
			LogUtils.e(TAG, iconid + "customdata==="+customdata);
			BaseUtils.playMedia(context,R.raw.knock);
			Intent pupInt = new Intent(context, ShowPopUp.class);
			pupInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
			pupInt.putExtra("data_push", message1);
			pupInt.putExtra("customdata", customdata);
			context.getApplicationContext().startActivity(pupInt);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//
		
		
	}

	

	public static int getStatusPush(Context context) {
		final SharedPreferences preferences = context.getSharedPreferences(TAGA, 0);
		return preferences.getInt(TAGA, NORMAl);
	}

	public static void saveStatusPush(Context context, int eNABLE2) {
		final SharedPreferences preferences = context.getSharedPreferences(TAGA, 0);
		preferences.edit().putInt(TAGA, eNABLE2).commit();
	}

	public static int NORMAl = 0;
	public static int ENABLE = 1;
	public static int CANCEL = 2;
	static final String TAGA = "register-push";
	public static void getDeviceToken(final Context context) {
		try {
			final String SENDER_ID = "209107842765";
			final GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
			if ( "".equalsIgnoreCase(KnockApplication.token_device_gcm)){
				new AsyncTask<Void, Void, String>() {
					@Override
					protected String doInBackground(Void... _params) {
						try {
							String regid = gcm.register(SENDER_ID);
							String deviceId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
							LogUtils.e(TAGA, "regid:" + regid + "==::device::" + deviceId);
							KnockApplication.token_device_gcm = regid;
						} catch (IOException ex) {
		
						}
						return null;
					}
		
					@Override
					protected void onPostExecute(String msg) {
					}
				}.execute(null, null, null);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
